﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataLib.DAL
{
    using System;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using DataLib.DBUtility;


    /// <summary>
    /// 生产相关操作类
    /// </summary>
    public partial class Productions
    {



        /// <summary>
        /// 生产提交
        /// <param name="outid">出库单id</param>
        /// </summary>
        public bool publicSubmit(int outid)
        {
            bool b = false;


            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@outid",SqlDbType.Int)
             };
            sqlparameter[0].Value = outid;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProuctionSubmitAudio", sqlparameter);
            dt.Load(dr);
            dr.Close();

            if (dt != null && dt.Rows.Count > 0)
            {
                if (int.Parse(dt.Rows[0][0].ToString()) > 0)
                    b = true;
            }
            return b;
        }


        /// <summary>
        /// 获取某段时间内的生产库存
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <param name="stroageno">仓库编号</param>
        /// <returns></returns>
        public DataTable GetStorageTotal(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,30),
                new SqlParameter("@GraphiCity",SqlDbType.NVarChar,30),
                new SqlParameter("@StartDate",SqlDbType.DateTime,30),
                new SqlParameter("@EndDate",SqlDbType.DateTime,30),
                new SqlParameter("@goodsCode",SqlDbType.NVarChar,30),
                new SqlParameter("@StorageNo",SqlDbType.NVarChar,30)
             };
            sqlparameter[0].Value = strbelong;
            sqlparameter[1].Value = strdis;
            sqlparameter[2].Value = startdate;
            sqlparameter[3].Value = enddate;
            sqlparameter[4].Value = gcode;
            sqlparameter[5].Value = stroageno;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProuectionStorageTotal", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;

        }

        /// <summary>
        /// 获取某段时间内的生产库存明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">仓库所属城市</param>
        /// <param name="strdis">仓库地理位置</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="stroageno">库位号</param>
        /// <param name="flag">明细类型</param>
        /// <returns></returns>
        public DataTable GetStorageItem(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno, int flag)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,30),
                new SqlParameter("@GraphiCity",SqlDbType.NVarChar,30),
                new SqlParameter("@StartDate",SqlDbType.NVarChar,30),
                new SqlParameter("@EndDate",SqlDbType.NVarChar,30),
                new SqlParameter("@goodsCode",SqlDbType.NVarChar,30),
                new SqlParameter("@StorageNo",SqlDbType.NVarChar,30),
                new SqlParameter("@flag",SqlDbType.Int)

             };
            sqlparameter[0].Value = strbelong;
            sqlparameter[1].Value = strdis;
            sqlparameter[2].Value = startdate;
            sqlparameter[3].Value = enddate;
            sqlparameter[4].Value = gcode;
            sqlparameter[5].Value = stroageno;
            sqlparameter[6].Value = flag;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProductionStorageItems", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;
        }
        /// <summary>
        /// 领料提交
        /// </summary>
        /// <param name="insid"></param>
        /// <param name="outid"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public int LingliaoSubmit(int insid, int outid, string month)
        {
            int result = 0;
            int rowcount;
            SqlParameter[] parameters = {
					new SqlParameter("@insid", SqlDbType.Int),
					new SqlParameter("@outid", SqlDbType.Int),
                     new SqlParameter("@month", SqlDbType.VarChar,20) };
            parameters[0].Value = insid;
            parameters[1].Value = outid;
            parameters[2].Value = month;

            result = DbHelperSQL.RunProcedure("EWMS_LingliaoSubmit", parameters, out rowcount);
            return result;
        }

        /// <summary>
        /// 2015年6月21日 FIFA
        /// 获取待生产入库列表，从出库单过来
        /// </summary>
        /// <param name="belongcity">归属地</param>
        /// <param name="grphiccity">处理地</param>
        /// <returns></returns>
        public DataTable getProduceWaitInStorageList(string belongcity, string graphiccity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,20),
                new SqlParameter("@GraphicCity",SqlDbType.VarChar,20)
              
             };
            sqlparameter[0].Value = belongcity;
            sqlparameter[1].Value = graphiccity;

            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetProduceWaitInSList", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;
        }


        /// <summary>
        /// 
        /// 维护列表
        /// </summary>
        /// <param name="belongcity">归属地</param>
        /// <param name="grphiccity">处理地</param>
        /// <returns></returns>
        public DataTable getProduceInSList(string belongcity, string graphiccity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,20),
                new SqlParameter("@GraphicCity",SqlDbType.VarChar,20)
              
             };
            sqlparameter[0].Value = belongcity;
            sqlparameter[1].Value = graphiccity;

            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProduceInSList", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;
        }

        /// <summary>
        /// 2015年7月1日 FIFA
        /// 获取待审核列表，从出库单过来
        /// </summary>
        /// <param name="belongcity">归属地</param>
        /// <param name="grphiccity">处理地</param>
        /// <returns></returns>
        public DataTable getProduceWaitInStorageListAudit(string belongcity, string graphiccity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,20),
                new SqlParameter("@GraphicCity",SqlDbType.VarChar,20)
              
             };
            sqlparameter[0].Value = belongcity;
            sqlparameter[1].Value = graphiccity;

            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetProduceWaitInSListAudit", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;
        }



        /// <summary>
        /// 获取某一时间的库存明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <returns></returns>
        public DataTable StorageItemsQuery(string enddate, string bcity, string gcity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@bcity",SqlDbType.VarChar,20),
                new SqlParameter("@gcity",SqlDbType.VarChar,20),
                new SqlParameter("@enddate",SqlDbType.DateTime,30),
             };
            sqlparameter[0].Value = bcity;
            sqlparameter[1].Value = gcity;
            sqlparameter[2].Value = enddate;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_Production_StoageItemsQuery", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;

        }

        /// <summary>
        /// 根据出生产生的ID取得生产返还单的ID
        /// </summary>
        /// <param name="outid">出库生产单的ID</param>
        /// <returns></returns>
        public Model.InStorage GetRsidByOutid(int outid)
        {
            int insid = 0;
            DataTable dt = new DataTable();
            InStorage ins = new InStorage();
            Model.InStorage model = new Model.InStorage();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from InStorage where outid=" + outid.ToString() + " and LEFT(wrlno,2)='RS'");

            dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                insid = int.Parse(dt.Rows[0][0].ToString());
                model = ins.GetModel(insid);
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 判断重量是否相等 ,相等则可提交 返还总重量+损耗重量=出库生产总重量+辅料总重量
        /// </summary>
        /// <param name="weight"></param>
        /// <param name="insid"></param>
        /// <param name="weighttype">1是Barcode，2是辅料，3是损耗</param>
        /// <returns></returns>
        public bool GetConfirm(decimal weight, int outid, int insid, int weighttype)
        {
            bool flag = false;
            decimal barweight = GetWeightByInsId(insid, outid, 1);
            decimal flweight = GetWeightByInsId(insid, outid, 2);
            decimal shweight = GetWeightByInsId(insid, outid, 3);
            decimal outsweight = GetWeightByInsId(insid, outid, 4);
            switch (weighttype)
            {
                case 1:
                    barweight = barweight + weight;
                    break;
                case 2:
                    flweight = flweight + weight;
                    break;
                case 3:
                    shweight = shweight + weight;
                    break;
                default:
                    break;
            }
            if ((barweight + shweight) == (flweight + outsweight))
                flag = true;
            return flag;
        }


        /// <summary>
        /// 取得整单总重量
        /// </summary>
        /// <param name="outid">出库生产的ID</param>
        /// <param name="insid">入库ID</param>
        /// <param name="weighttype">1是Barcode，2是辅料，3是损耗,4是出库单重量</param>
        /// <returns></returns>
        private decimal GetWeightByInsId(int insid, int outid, int weighttype)
        {
            decimal objweight = 0;
            object obj = null;
            string strsql = "";
            switch (weighttype)
            {
                case 1:
                    strsql = "select sum(nwt) from barcode where isdel_out=0 and InStrorageID=" + insid.ToString();
                    break;
                case 2:
                    strsql = "select sum(nwt) from ins_fuliao where instroageid=" + insid.ToString();
                    break;
                case 3:
                    strsql = "select sum(nwt) from ins_sunhao where instroageid=" + insid.ToString();
                    break;
                case 4:
                    strsql = "select sum(nwt) from barcode where isdel_out=0 and InStrorageID= " + outid.ToString();
                    break;
                default:
                    break;
            }

            DataTable dt = DbHelperSQL.Query(strsql).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                obj = dt.Rows[0][0];
            }
            try
            {
                objweight = decimal.Parse(obj.ToString());
            }
            catch
            {
                objweight = 0;
            }
            return objweight;
        }

        /// <summary>
        /// 获得返还单录入的Barcode
        /// </summary>
        public DataSet GetBarcodeList(int outid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from BarCode bar,InStorage ins ");
            strSql.Append(" where bar.InStrorageID=ins.id ");
            strSql.Append(" and Left(ins.wrlno,2)='RB' ");
            strSql.Append(" and ins.outno='" + outid.ToString() + "'");

            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得返还单录入的Barcode
        /// <param name="outid">出库单id</param>
        /// <param name="instype">入库单类型 生产领料或者返还</param>
        /// </summary>
        public DataSet GetBarcodeList(int outid, string instype)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from BarCode bar,InStorage ins ");
            strSql.Append(" where bar.InStrorageID=ins.id ");
            strSql.Append(" and Left(ins.wrlno,2)='" + instype + "' ");
            strSql.Append(" and ins.outid=" + outid.ToString());

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 取得领料单的总重量
        /// </summary>
        /// <param name="insid"></param>
        /// <param name="nwt"></param>
        /// <returns></returns>
        public decimal GetInsTotal(string insid)
        {
            DataTable dt = new DataTable();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ISNULL(SUM(nwt),0) from BarCode bar ");
            strSql.Append(" where InStrorageID=" + insid);
            dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            decimal instotal = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                instotal = decimal.Parse(dt.Rows[0][0].ToString());
            }
            return instotal;

        }

        /// <summary>
        /// 取得barcode列表领料单或者返还单
        /// </summary>
        /// <param name="outid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataTable bindBarcodeList(int outid, string type)
        {
            DataTable dt = new DataTable();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from BarCode where InStrorageID=");
            strSql.Append(" (select id from InStorage where ");
            if (type == "S")
            {
                strSql.Append("outno='" + outid.ToString() + "'");
            }
            else
            {
                strSql.Append("outid=" + outid.ToString());
            }
            strSql.Append(" LEFT(wrlno,1)='" + type + "'");
            dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public List<Model.sc_xh> GetSHList(string outid)
        {
            StringBuilder strSql = new StringBuilder();
            List<Model.sc_xh> list = new List<Model.sc_xh>();
            strSql.Append("select * from sc_xh where outid =  " + outid.ToString());
            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            if (dt != null)
            {
                Model.sc_xh model = null;
                sc_xh dal = new sc_xh();
                foreach (DataRow dr in dt.Rows)
                {
                    model = dal.DataRowToModel(dr);
                    list.Add(model);
                    model = null;
                }
            }
            return list;
        }

        /// <summary>
        /// 删除领料库存
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelBarCodeMaterial(int id)
        {
            int result = 0;
            BarCode bardal = new BarCode();
            StorageData sddal = new StorageData();
            Model.BarCode bar = new Model.BarCode();
            bar = bardal.GetModel(id);
            if (bar == null)
            {
                result = -1;
            }
            else if (bar.status == -1)
            {
                bardal.Delete(id);
                result = 1;
            }
            else if (bar.status == 14)
            {
                result = 0;
            }
            else
            {

                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from BarCode ");
                strSql.Append(" where id=@id");
                SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
                parameters[0].Value = id;

                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    strSql.Clear();
                    strSql.Append("delete from StorageData ");
                    strSql.Append(" where barcodeid=@id");
                    SqlParameter[] parameters1 = {
					new SqlParameter("@id", SqlDbType.Int,4)        };
                    parameters1[0].Value = id;

                    rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters1);
                    if (rows > 0)
                        result = 1;
                    else
                        result = -2;
                }
                else
                {
                    result = -3;
                }
            }
            return result;
        }
        ///// <summary>
        ///// 获得返还单录入的Barcode
        ///// </summary>
        //public Model.sc_xh GetScModel(int outid)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select * from BarCode bar,InStorage ins ");
        //    strSql.Append(" where bar.InStrorageID=ins.id ");
        //    strSql.Append(" and Left(ins.wrlno,1)='G' ");
        //    strSql.Append(" and ins.outno='" + outid.ToString() + "'");

        //    return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        //}


        /// <summary>
        /// 入库单插入或者修改
        /// </summary>
        /// <returns></returns>
        public int Save(Model.InStorage ins)
        {
            SqlParameter[] sqlparameter ={
                new SqlParameter("@cjoid",SqlDbType.Int),
                new SqlParameter("@wrlno",SqlDbType.VarChar,30),
                new SqlParameter("@instoragetime",SqlDbType.DateTime),
                new SqlParameter("@outid",SqlDbType.Int),
               new SqlParameter("@outno",SqlDbType.VarChar,20),
                new SqlParameter("@Operator",SqlDbType.VarChar,20),
               new SqlParameter("@InDepart",SqlDbType.VarChar,20),
                new SqlParameter("@OutDepart",SqlDbType.VarChar,20),
               new SqlParameter("@status",SqlDbType.Int),
                new SqlParameter("@instotal",SqlDbType.Decimal,15),
               new SqlParameter("@loadnumid",SqlDbType.Int),
                new SqlParameter("@mechanerr",SqlDbType.Decimal,15),
                new SqlParameter("@vloadnum",SqlDbType.VarChar,20),
                new SqlParameter("@clearingmonth",SqlDbType.VarChar,6),
                new SqlParameter("@islock",SqlDbType.Bit),
                new SqlParameter("@ErrorMessage",SqlDbType.VarChar,200)
            
             };
            sqlparameter[0].Value = ins.cjoid;
            sqlparameter[1].Value = ins.wrlno;

            sqlparameter[2].Value = ins.instoragetime;
            sqlparameter[3].Value = ins.outid;
            sqlparameter[4].Value = ins.outno;
            sqlparameter[5].Value = ins.Operator;
            sqlparameter[6].Value = ins.InDepart;
            sqlparameter[7].Value = ins.OutDepart;
            sqlparameter[8].Value = ins.status;
            sqlparameter[9].Value = ins.instotal;
            sqlparameter[10].Value = ins.loadnumid;
            sqlparameter[11].Value = ins.mechanerr;
            sqlparameter[12].Value = ins.vloadnum;
            sqlparameter[13].Value = ins.clearingmonth;
            sqlparameter[14].Value = ins.islock;
            sqlparameter[15].Direction = ParameterDirection.Output;
            int insid = DbHelperSQL.RunProcedure("EWMS_ProductINSave", sqlparameter, out insid);

            return insid;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="outid"></param>
        /// <param name="strtype">W 代表 领料重量+辅料 S代表 入库重量+损耗 </param>
        /// <returns></returns>
        public string GetStrFormulae(int outid, string strtype)
        {
            string strweight = "";
            SqlParameter[] sqlparameter ={
                new SqlParameter("@outid",SqlDbType.Int),
                new SqlParameter("@strtype",SqlDbType.VarChar,5)
             };
            sqlparameter[0].Value = outid;
            sqlparameter[1].Value = strtype;

            DataTable dt = new DataTable();
            dt = DbHelperSQL.RunProcedure("EWMS_GetStrWeight", sqlparameter, "t").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                strweight = dt.Rows[0][0].ToString();
            }

            return strweight;
        }

        /// <summary>
        /// 根据outid返回生产返回的单
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public int GetInsIDByOutid(int outid)
        {
            int insid = 0;
            string strsql = "select top 1 id from InStorage where  LEFT(wrlno,2)='RS' and outid=" + outid.ToString() + " order by id desc";
            object obj = DbHelperSQL.GetSingle(strsql);
            if (obj == null)
            {
                insid = 0;
            }
            else
            {
                insid = int.Parse(obj.ToString());
            }
            return insid;
        }


        /// <summary>
        /// 辅料损耗编号
        /// </summary>
        /// <param name="insid">入库id</param>
        /// <param name="leftChar">H为损耗单号，J为辅料单号</param>
        /// <returns></returns>
        public string GetFSNoint(int insid, char leftChar)
        {
            string tabno = "";
            StringBuilder str = new StringBuilder();
            string fieldname = "";
            string serialType = "";
            int numlen = 7;
            int maxserial = 0;
            tab_Num t = new tab_Num();
            Model.InStorage m = new Model.InStorage();
            InStorage ins = new InStorage();
            m = ins.GetModel(insid);

            if (leftChar == 'H')
            {
                fieldname = "shno";
                serialType = "InsSNo";
            }
            else
            {
                fieldname = "flno";
                serialType = "InsFNo";
            }
            str.Append("select " + fieldname + " from InStorage where id=" + insid.ToString());
            DataTable dt = DbHelperSQL.Query(str.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0] != null && dt.Rows[0][0].ToString().Trim() != "")
                {
                    tabno = dt.Rows[0][0].ToString().Trim();
                }
                else
                {
                    maxserial = t.GetNoByType(serialType);
                    maxserial++;
                    tabno = leftChar + maxserial.ToString().PadLeft(numlen, '0');
                    if (leftChar == 'H')
                    {
                        m.shno = tabno;
                    }
                    else
                    {
                        m.flno = tabno;
                    }
                    ins.Update(m);

                }

            }

            return tabno;


        }



        /// <summary>
        /// 入库审核通过
        /// </summary>
        /// <returns></returns>
        public int SubmitAudio(int insid,string month)
        {
            SqlParameter[] sqlparameter ={
                new SqlParameter("@insid",SqlDbType.Int),
             
             };
            sqlparameter[0].Value = insid;
         

            DbHelperSQL.RunProcedure("EWMS_ProductionAudio", sqlparameter, "t1");

            return insid;
        }


                /// <summary>
        /// 录入提交 更改生产出库单状态
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public int LuRuSubmit(int outid)
        {
            int insid = 0;
            SqlParameter[] sqlparameter ={
                new SqlParameter("@outid",SqlDbType.Int)
             };
            sqlparameter[0].Value = outid;

            DbHelperSQL.RunProcedure("EWMS_ProductionSubmit", sqlparameter, "t1");
            insid = GetInsIDByOutid(outid);
            return insid;
            
        }


        /// <summary>
        /// 库存明细(从汇总过来)
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">仓库所属城市</param>
        /// <param name="strdis">仓库地理位置</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="stroageno">库位号</param>
        /// <param name="flag">明细类型</param>
        /// <returns></returns>
        public DataSet ProductionItems(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno, int flag)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,30),
                new SqlParameter("@GraphiCity",SqlDbType.NVarChar,30),
                new SqlParameter("@StartDate",SqlDbType.NVarChar,30),
                new SqlParameter("@EndDate",SqlDbType.NVarChar,30),
                new SqlParameter("@goodsCode",SqlDbType.NVarChar,30),
                new SqlParameter("@StorageNo",SqlDbType.NVarChar,30),
                new SqlParameter("@flag",SqlDbType.Int)

             };
            sqlparameter[0].Value = strbelong;
            sqlparameter[1].Value = strdis;
            sqlparameter[2].Value = startdate;
            sqlparameter[3].Value = enddate;
            sqlparameter[4].Value = gcode;
            sqlparameter[5].Value = stroageno;
            sqlparameter[6].Value = flag;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProuectionStorageItems", sqlparameter);
            dt.Load(dr);
            dr.Close();
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds;
        }


        /// <summary>
        /// 判断生产单的重量是否平衡
        /// </summary>
        /// <param name="outid">出库生产的ID</param>
        /// <param name="insid">入库ID</param>
        /// <returns>真为相等 假为不相等</returns>
        public bool GetFinshed(int insid, int outid)
        {
            bool flag = false;
            object obj = null;
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DECLARE @flnwt decimal(10,2);\n");
            strSql.Append("DECLARE @shnwt decimal(10,2);\n");
            strSql.Append("DECLARE @insnwt decimal(10,2);\n");
            strSql.Append("DECLARE @outsnwt decimal(10,2);\n");
            strSql.Append("set @flnwt = ISNULL((select SUM(nwt) from ins_fuliao where instroageid=" + insid.ToString() + ") ,0);\n");
            strSql.Append("set @shnwt = ISNULL((select SUM(nwt) from ins_sunhao where instroageid=" + insid.ToString() + ") ,0);\n");
            strSql.Append("set @outsnwt = ISNULL((select sum(nwt) from BarCode where OutStrorageID="+outid.ToString() +"),0);\n");
            strSql.Append("set @insnwt = ISNULL((select sum(nwt) from BarCode where InStrorageID=" + insid.ToString() + "),0);\n");
            strSql.Append("if @flnwt + @outsnwt = @insnwt + @shnwt\n");
            strSql.Append(" select 1;\n");
            strSql.Append("else\n");
            strSql.Append(" select 0;");

            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                obj = dt.Rows[0][0];
            }
            try
            {
                if (int.Parse(obj.ToString()) == 1)
                {
                    flag = true;
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
    }
}